<?php

    class ProjectController extends Controller {
        public $metaDescription = null;
        public $metaKeywords = null;
        public $linkCanoncical = null;
        public $title = null;
        public $layout = "column_project";

        public function actionIndex(){    
            $url = new Url();     
            $rowProject = array();
            if(!isset($_SESSION['user_pm'])){
                $this->redirect($url->createUrl("user/login"));
            }

            if(!isset($_GET['project']) && !isset($_SESSION['project_pm'])){
                $this->redirect($url->createUrl("user/login"));
            }

            if(!isset($_SESSION['project_pm'])){
                $project_alias = isset($_GET["project"])?mysql_escape_string($_GET["project"]):"";        
                $rowProject = Project::getRowByAlias($project_alias);
                $_SESSION["project_pm"]=$rowProject;                
            }

            if($_SESSION['project_pm']['project_alias'] != $_GET["project"]){
                $project_alias = isset($_GET["project"])?mysql_escape_string($_GET["project"]):"";        
                $rowProject = Project::getRowByAlias($project_alias);
                $_SESSION["project_pm"]=$rowProject;                
            }

            $data_member = Member::getRowByProjectId($_SESSION["project_pm"]['project_id']);
            $data_role = Role::getRowByProjectID($_SESSION["project_pm"]['project_id']);
            $data_log = Log::getRowByProjectID($_SESSION["project_pm"]['project_id']);
            $total_time = 0;
            foreach($data_log as $row){
                $total_time = $total_time + $row['log_hour'];
            }

            $rowProject = Project::getRowById($_SESSION["project_pm"]['project_id']);             

            $this->render('index',array("data_member"=>$data_member,"data_role"=>$data_role,"total_time"=>$total_time,"data_project"=>$rowProject));
        }
        
        public function actionInfo(){    
            $this->layout='column_main';
            $url = new Url();     
            $rowProject = array();                    

            $project_id = isset($_GET["project_id"])?mysql_escape_string($_GET["project_id"]):"";                    
            $rowProject = Project::getRowById($project_id);                              

            $data_member = Member::getRowByProjectId($rowProject['project_id']);
            $data_role = Role::getRowByProjectID($rowProject['project_id']);
            $data_log = Log::getRowByProjectID($rowProject['project_id']);
            $total_time = 0;
            foreach($data_log as $row){
                $total_time = $total_time + $row['log_hour'];
            }

            $rowProject = Project::getRowById($rowProject['project_id']);             

            $this->render('info',array("data_member"=>$data_member,"data_role"=>$data_role,"total_time"=>$total_time,"data_project"=>$rowProject));
        }

        public function actionDetail(){        
            $url = new Url();     
            $this->render('detail',array());
        }

        public function actionAddMember(){        
            $url = new Url();     
            $project_id = $_SESSION['project_pm']['project_id'];
            $data_role = Role::getRowByProjectID($project_id);        
            $data_user = User::getAllRows();
            $data_member = Member::getRowByProjectId($project_id);            
            $this->render('add_member',array("data_role"=>$data_role,"data_user"=>$data_user,"data_member"=>$data_member));
        }

        public function actionList(){        
            $this->layout='column_main';
            $url = new Url();     
            $closed = 0;
            if(isset($_GET['closed'])){
                $closed = 1;
            }
            $data = Project::getAllRows($closed);

            $this->render('list',array("data"=>$data));
        }

        public function actionAdd(){        
            $this->layout='column_main';
            $url = new Url();     
            $this->render('add',array());
        }

        public function actionAjaxAddProject(){            

            $project_name = isset($_GET["project_name"])?mysql_escape_string($_GET["project_name"]):"";
            $project_description = isset($_GET["project_description"])?mysql_escape_string($_GET["project_description"]):"";
            $project_identifier = isset($_GET["project_identifier"])?mysql_escape_string($_GET["project_identifier"]):"";
            $project_homepage = isset($_GET["project_homepage"])?mysql_escape_string($_GET["project_homepage"]):"";            
            $project_public = isset($_GET["project_public"])?mysql_escape_string($_GET["project_public"]):"";            
            $project_budget = isset($_GET["project_budget"])?mysql_escape_string($_GET["project_budget"]):"";            
            $project_company = isset($_GET["project_company"])?mysql_escape_string($_GET["project_company"]):"";            
            $project_customer = isset($_GET["project_customer"])?mysql_escape_string($_GET["project_customer"]):"";            
            $project_start_date = isset($_GET["project_start_date"])?mysql_escape_string($_GET["project_start_date"]):"";            
            $project_end_date = isset($_GET["project_end_date"])?mysql_escape_string($_GET["project_end_date"]):"";            
            $filepath = isset($_GET["filepath"])?mysql_escape_string($_GET["filepath"]):"";            

            $rowProject = Project::getRowByAlias($project_identifier);

            if($rowProject){
                echo -3;exit;
            }            

            $array_input['project_name'] = $project_name;
            $array_input['project_alias'] = $project_identifier;
            $array_input['project_description'] = $project_description;
            $array_input['project_homepage'] = $project_homepage;
            $array_input['project_public'] = $project_public;
            $array_input['project_status'] = 1;
            $array_input['project_image'] = $filepath;
            $array_input['project_budget'] = $project_budget;
            $array_input['project_remain'] = $project_budget;
            $array_input['project_company'] = $project_company;
            $array_input['project_customer'] = $project_customer;
            $array_input['project_start_date'] = strtotime(DateUtils::convertDatedb($project_start_date));
            $array_input['project_end_date'] = strtotime(DateUtils::convertDatedb($project_end_date));
            $array_input['project_create_date'] = time();
            $array_input['project_admin_id'] = $_SESSION['user_pm']['user_id'];
            $last_id = Project::insertObject($array_input);

            $array_activity['project_id'] = $last_id;
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name được khởi tạo";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxUpdateProject(){            

            $project_name = isset($_GET["project_name"])?mysql_escape_string($_GET["project_name"]):"";
            $project_description = isset($_GET["project_description"])?mysql_escape_string($_GET["project_description"]):"";
            $project_id = isset($_GET["project_id"])?mysql_escape_string($_GET["project_id"]):"";
            $project_homepage = isset($_GET["project_homepage"])?mysql_escape_string($_GET["project_homepage"]):"";            
            $project_public = isset($_GET["project_public"])?mysql_escape_string($_GET["project_public"]):"";            
            $project_budget = isset($_GET["project_budget"])?mysql_escape_string($_GET["project_budget"]):"";            
            $project_company = isset($_GET["project_company"])?mysql_escape_string($_GET["project_company"]):"";            
            $project_customer = isset($_GET["project_customer"])?mysql_escape_string($_GET["project_customer"]):"";            
            $project_image = isset($_GET["project_image"])?mysql_escape_string($_GET["project_image"]):"";
            $project_start_date = isset($_GET["project_start_date"])?mysql_escape_string($_GET["project_start_date"]):"";            
            $project_end_date = isset($_GET["project_end_date"])?mysql_escape_string($_GET["project_end_date"]):"";            

            $array_input['project_name'] = $project_name;        
            $array_input['project_description'] = $project_description;
            $array_input['project_homepage'] = $project_homepage;
            $array_input['project_public'] = $project_public;
            $array_input['project_budget'] = $project_budget;            
            $array_input['project_remain'] = $project_budget;            
            $array_input['project_company'] = $project_company;            
            $array_input['project_image'] = $project_image;            
            $array_input['project_customer'] = $project_customer;            
            $array_input['project_start_date'] = strtotime(DateUtils::convertDatedb($project_start_date));
            $array_input['project_end_date'] = strtotime(DateUtils::convertDatedb($project_end_date));

            $update = Project::updateObject($array_input,"project_id",$project_id);

            $rowProject = Project::getRowById($project_id);
            $_SESSION["project_pm"]=$rowProject;                

            $array_activity['project_id'] = $project_id;
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name thay đổi thông tin dự án";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($update !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxUpdateStatusProject(){            

            $project_status = isset($_GET["status"])?mysql_escape_string($_GET["status"]):"";                
            $reason = isset($_GET["reason"])?mysql_escape_string($_GET["reason"]):"";
            $array_input['project_status'] = $project_status;        

            $update = Project::updateObject($array_input,"project_id",$_SESSION['project_pm']['project_id']);

            $rowProject = Project::getRowById($_SESSION['project_pm']['project_id']);
            $_SESSION["project_pm"]=$rowProject;                

            $project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name thay đổi trạng thái dự án";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            $data_member = Member::getRowByProjectId($rowProject['project_id']);
            foreach($data_member as $row){
                $project_name = $rowProject['project_name'];
                $messege = "Dự án $project_name đã được thay đổi trạng thái! <br> Lý do : $reason";
                $mail = $row['email'];                
                SendMail::sendMail($mail,$messege);                   
            }            
            
            if($update !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionVersion(){        
            $url = new Url();     
            $project_id = $_SESSION['project_pm']['project_id'];
            $data = Version::getRowByProjectID($project_id);        
            $this->render('version',array("data"=>$data));
        }

        public function actionAjaxUpdateVersion(){            

            $version_id = isset($_GET["id"])?mysql_escape_string($_GET["id"]):"";
            $version_name = isset($_GET["version_name"])?mysql_escape_string($_GET["version_name"]):"";
            $version_description = isset($_GET["version_description"])?mysql_escape_string($_GET["version_description"]):"";
            $version_status = isset($_GET["version_status"])?mysql_escape_string($_GET["version_status"]):"";
            $create_date = isset($_GET["create_date"])?mysql_escape_string($_GET["create_date"]):"";                    

            $array_input['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_input['version_name'] = $version_name;
            $array_input['version_description'] = $version_description;        
            $array_input['version_status'] = $version_status;
            $array_input['version_create_date'] = strtotime(DateUtils::convertDatedb($create_date));

            if($version_id == 0){
                $last_id = Version::insertObject($array_input);

                $project_name = $_SESSION['project_pm']['project_name'];
                $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
                $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
                $array_activity['activity_name'] = "Dự án $project_name thêm mới phiên bản $version_name";
                $array_activity['activty_type'] = 1;
                $array_activity['activity_update_time'] = time();        
                $log = Activity::insertObject($array_activity);       
            }else{
                $last_id = Version::updateObject($array_input,"version_id",$version_id);
            }            

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxDeleteVersion(){            

            $version_id = isset($_GET["version_id"])?mysql_escape_string($_GET["version_id"]):"";

            $array_input['version_id'] = $version_id;    
            $last_id = Version::deleteObject($array_input);

            $project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name xóa phiên bản";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionCategory(){        
            $url = new Url();     
            $project_id = $_SESSION['project_pm']['project_id'];
            $data = Category::getRowByProjectID($project_id);        
            $this->render('category',array("data"=>$data));
        }

        public function actionAjaxUpdateCategory(){            

            $category_id = isset($_GET["id"])?mysql_escape_string($_GET["id"]):"";
            $category_name = isset($_GET["category_name"])?mysql_escape_string($_GET["category_name"]):"";
            $category_description = isset($_GET["category_description"])?mysql_escape_string($_GET["category_description"]):"";

            $array_input['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_input['category_name'] = $category_name;
            $array_input['category_description'] = $category_description;    

            if($category_id == 0){
                $last_id = Category::insertObject($array_input);

                $project_name = $_SESSION['project_pm']['project_name'];
                $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
                $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
                $array_activity['activity_name'] = "Dự án $project_name thêm mới danh mục $category_name";
                $array_activity['activty_type'] = 1;
                $array_activity['activity_update_time'] = time();        
                $log = Activity::insertObject($array_activity);    
            }else{
                $last_id = Category::updateObject($array_input,"category_id",$category_id);
            }                            

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxAddMember(){            

            $role_id = isset($_GET["role_id"])?mysql_escape_string($_GET["role_id"]):"";
            $selected = isset($_GET["selected"])?mysql_escape_string($_GET["selected"]):"";

            $array_user_id = split(',',$selected);        

            foreach($array_user_id as $row){
                $array_input['project_id'] = $_SESSION['project_pm']['project_id'];        
                $array_input['role_id'] = $role_id;
                $array_input['user_id'] = $row;                   
                $last_id = Member::insertObject($array_input);
            }        

            $project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name thêm thành viên";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxDeleteCategory(){            

            $category_id = isset($_GET["category_id"])?mysql_escape_string($_GET["category_id"]):"";

            $array_input['category_id'] = $category_id;    
            $last_id = Category::deleteObject($array_input);

            $project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name xóa danh mục";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionRole(){        
            $url = new Url();     
            $project_id = $_SESSION['project_pm']['project_id'];
            $data = Role::getRowByProjectID($project_id);        
            $this->render('role',array("data"=>$data));
        }

        public function actionAjaxUpdateRole(){            

            $role_id = isset($_GET["id"])?mysql_escape_string($_GET["id"]):"";
            $role_name = isset($_GET["role_name"])?mysql_escape_string($_GET["role_name"]):"";
            $role_description = isset($_GET["role_description"])?mysql_escape_string($_GET["role_description"]):"";

            $array_input['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_input['role_name'] = $role_name;
            $array_input['role_description'] = $role_description;       
            if($role_id == 0){
                $last_id = Role::insertObject($array_input);

                $project_name = $_SESSION['project_pm']['project_name'];
                $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
                $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
                $array_activity['activity_name'] = "Dự án $project_name thêm mới chức vụ $role_name";
                $array_activity['activty_type'] = 1;
                $array_activity['activity_update_time'] = time();        
                $log = Activity::insertObject($array_activity);                        
            }else{
                $last_id = Role::updateObject($array_input,"role_id",$role_id);
            }                     

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxDeleteRole(){            

            $role_id = isset($_GET["role_id"])?mysql_escape_string($_GET["role_id"]):"";

            $array_input['role_id'] = $role_id;    
            $last_id = Role::deleteObject($array_input);

            $project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name xóa chức vụ";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxDeleteMember(){            

            $member_id = isset($_GET["member_id"])?mysql_escape_string($_GET["member_id"]):"";

            $array_input['member_id'] = $member_id;        
            $last_id = Member::deleteObject($array_input);

            $project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name xóa thành viên";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

    }

?>
